/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.org.ccb.ccbadm.model;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author alanoliveira
 */
@Entity
@Table(name = "usuario")
//mysql não suporta sequencia ...
//@SequenceGenerator(name = "usuario_seq", sequenceName = "usuario_seq", allocationSize = 1)
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"),
    @NamedQuery(name = "Usuario.findById", query = "SELECT u FROM Usuario u WHERE u.id = :id"),
    @NamedQuery(name = "Usuario.findByLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login")})

public class Usuario implements Serializable {
 
    @JoinTable(name = "usuario_administracao", joinColumns = {
        @JoinColumn(name = "usuario_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "administracao_id", referencedColumnName = "id")})
    @ManyToMany  
    private List<Administracao> administracaoList;  
 
    @JoinTable(name = "usuario_grupo", joinColumns = {
        @JoinColumn(name = "usuario_id", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "grupo_id", referencedColumnName = "id")})
    @ManyToMany  
    private List<Grupo> grupoList;   
    
    @Id
    @NotNull    
    @Basic(optional = false)
    //@GeneratedValue(generator="usuario_seq", strategy = GenerationType.SEQUENCE)      
    @Column(name = "id")
    private Integer id;
    @NotNull    
    @Basic(optional = false)
    @Size(min = 1, max = 20)
    @Column(name = "login", unique = true)
    private String login;
    @NotNull    
    @Basic(optional = false)
    @Size(min = 1, max = 100)
    @Column(name = "senha")
    private String senha;  
    @JoinColumn(name = "pessoa_fisica_id", referencedColumnName = "id")
    @OneToOne(fetch= FetchType.EAGER)
    private PessoaFisica pessoaFisicaId;
    @Column(name = "desativado", nullable = false)
    private Boolean desativado;
    
    public Usuario() {
        
    }

    public Usuario(Integer id) {
        this.id = id;
    }

    public Usuario(Integer id, String login, String senha, long idpessoa) {
        this.id = id;
        this.login = login;
        this.senha = senha;
       // this.idpessoa = idpessoa;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }


    public PessoaFisica getPessoaFisicaId() {
        return pessoaFisicaId;
    }

    public void setPessoaFisicaId(PessoaFisica pessoaFisicaId) {
        this.pessoaFisicaId = pessoaFisicaId;
    }

    public Boolean isDesativado() {
        return desativado;
    }

    public void setDesativado(Boolean desativado) {
        this.desativado = desativado;
    }
    
    @XmlTransient
    public List<Administracao> getAdministracaoList() {
       return administracaoList;
    }

    public void setAdministracaoList(List<Administracao> administracaoList) {
        this.administracaoList = administracaoList;
    }  

    public List<Grupo> getGrupoList() {
        return grupoList;
    }

    public void setGrupoList(List<Grupo> grupoList) {
        this.grupoList = grupoList;
    }    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Usuario other = (Usuario) obj;
        if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
            return false;
        }
        if (this.pessoaFisicaId != other.pessoaFisicaId && (this.pessoaFisicaId == null || !this.pessoaFisicaId.equals(other.pessoaFisicaId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Usuario{" + "id=" + id + ", login=" + login + ", desativado=" + desativado + '}';
    }


    public Iterable<Administracao> getAdministracao() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
